package edu.iscas.xcheck.analysis.dom.utils;

import java.util.HashMap;
import java.util.Map;

import edu.iscas.xcheck.analysis.dom.model.BrowserEvent;
import edu.iscas.xcheck.analysis.dom.model.StateEdge;
import edu.iscas.xcheck.analysis.model.Platform;

public class StateEdgeUtil {
	private HashMap<String,StateEdge> originMap = new HashMap<String, StateEdge>();
	private Map<Platform, Map<String, StateEdge>> platStateMap = new HashMap<>();
	public StateEdgeUtil(){
		StateEdge loadState = new StateEdge();
		loadState.setFromId(-1);
		loadState.setToId(0);
		loadState.setEventType("load");
		originMap.put("load", loadState);
	}
	/**
	 * 用于保存当前event状态
	 */
	public int fromState = -1;
	/**
	 * -2表示event为null 如果匹配失败返回-1，匹配成功且未检测过返回0，匹配成功，检测过否者返回1,多出来的中间状态返回2
	 * @param event
	 * @return
	 */
	public int repeatCheck(BrowserEvent event){
		String eventKey = getEventKey(event);
		if (null != eventKey) {
			if(eventKey.equals("load")){
				event.setState(0);
				fromState = 0;
				return 0;
			}
			StateEdge se = originMap.get(eventKey);
			if (null == se) {
				//不需要检测
				return -1;
			}else{
				return 0;
			}
//			//如果是第一个状态,检测过，返回1，未检测返回0，这个时候都需要更新当前状态fromState为新事件的状态
//			//如果不是第一个状态，查看是否是从fromState转换过来
//			//如果当前状态是最后的状态，即toid为0
//			event.setState(se.getToId());
//			if (0 == se.getFromId() || fromState == se.getFromId()) {
//				fromState = se.getToId();
//				boolean result = se.isChecked();
//				se.setChecked(true);
//				return result?1:0;
//			}else {
//				return 2;
//			}
//			//event.setStateEdge(state);
		}
		return -2;
	}
	public int checkState(BrowserEvent event, Platform platform){
		String eventKey = getEventKey(event);
		if (null != eventKey) {
			Map<String, StateEdge> stateMap = platStateMap.get(platform);
			
			StateEdge se = stateMap.get(eventKey);
			if(se != null){
				stateMap.remove(eventKey);
				event.setState(se.getToId());
				fromState = se.getToId();
				return 0;
			}else if(eventKey.equals("setTimeout")){
				return 0;
			}
		}
		return 0;//-1;
	}
//	public void resetCheck(){
//		fromState = 0;
//		for (StateEdge se : originMap.values()) {
//			se.setChecked(false);
//		}
//	}
	public void pushStateEdge(StateEdge se){
		if(null != se){
			
			originMap.put(se.getEventId().replace("[1]/", "/")+se.getEventType(), se);
		}
	}
	private String getEventKey(BrowserEvent event){
		StringBuffer eventBuffer = new StringBuffer();
		if (null != event ) {
			if (null != event.getPath()) {
				eventBuffer.append(event.getPath());
			}
			if (null != event.getType()) {
				eventBuffer.append(event.getType());
			}
		}
		return (0 == eventBuffer.length())?null:eventBuffer.toString();
	}
	@SuppressWarnings("unchecked")
	public void addBrowser(Platform platform){
		Map<String, StateEdge> stateMap = (Map<String, StateEdge>) originMap.clone();
		platStateMap.put(platform, stateMap);
	}
//	@Override
//	public Object clone() throws CloneNotSupportedException{
//		StateEdgeUtil seUtil = new StateEdgeUtil();
//		for(StateEdge value:this.stateMap.values()){
//			seUtil.pushStateEdge(value);
//		}
//		return seUtil;
//	}
}
